package com.itheima.sc.impl.sms;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.sc.api.sms.SmsFlashPromotionSessionApi;
import com.itheima.sc.config.ComException;
import com.itheima.sc.mapper.sms.SmsFlashMapper;
import com.itheima.sc.mapper.sms.SmsFlashProductRelationMapper;
import com.itheima.sc.mapper.sms.SmsFlashPromotionSessionMapper;
import com.itheima.sc.pojo.sms.FlashPromotion;
import com.itheima.sc.pojo.sms.FlashPromotionProductRelation;
import com.itheima.sc.pojo.sms.FlashPromotionSession;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class SmsFlashPromotionSessionImpl implements SmsFlashPromotionSessionApi {

    @Autowired
    private SmsFlashPromotionSessionMapper sessionMapper;

    @Autowired
    private SmsFlashProductRelationMapper relationMapper;

    @Autowired
    private SmsFlashMapper smsFlashMapper;


    //判断session的id是否存在
    public boolean existSession(Long flashPromotionSessionId) {
        //判断id查询出的数据是否为空
        FlashPromotionSession se = sessionMapper.selectById(flashPromotionSessionId);
        return ObjectUtil.isNotEmpty(se) ? true : false;
    }

    //添加场次数据
    public void addFlashSession(FlashPromotionSession promotionSession) {
        sessionMapper.insert(promotionSession);
    }

    //删除场次信息 遇到删除三思后行
    public void deleteFlashSession(Long id) {
        sessionMapper.deleteById(id);
    }

    //获取场次的状态
    public int findStatus(Long id) {
        //1.要查询在秒杀活动中是否有使用此场次 id作为场次id
        LambdaQueryWrapper<FlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FlashPromotionProductRelation::getFlashPromotionSessionId, id);
        List<FlashPromotionProductRelation> relations = relationMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(relations)) {
          /*  //1.通过商品活动关联数据查询是否存在活动
            List<Long> flashPromotionIds = CollUtil.getFieldValues(relations, "flashPromotionId", Long.class);
            List<FlashPromotion> flashPromotions = smsFlashMapper.selectBatchIds(flashPromotionIds);
            if (CollUtil.isNotEmpty(flashPromotions)) {
                //秒杀活动存在 删除秒杀的活动
                List<Long> flashIds = CollUtil.getFieldValues(flashPromotions, "id", Long.class);
                smsFlashMapper.deleteBatchIds(flashIds);
            }
             */

            //2.存在数据  先删除活动关联数据
            List<Long> ids = CollUtil.getFieldValues(relations, "id", Long.class);
            relationMapper.deleteBatchIds(ids);
        }
        //不存在数据  先查看其状态,是否以启用  直接删除
        FlashPromotionSession session = sessionMapper.selectById(id);
        return session.getStatus();
    }

    //获取全部场次的信息
    public List<FlashPromotionSession> getFlashSessionList() {
        return sessionMapper.selectList(null);
    }

    //获取全部可选场次及其数量
    public List<FlashPromotionSession> getFlashSessionSelectList(Integer flashPromotionId) {
        //1.通过关联关系表去查询所有的场次的ids
        LambdaQueryWrapper<FlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        List<FlashPromotionProductRelation> relations = relationMapper.selectList(wrapper);
        List<Long> sessionIds = CollUtil.getFieldValues(relations, "flashPromotionSessionId", Long.class);
        if (CollUtil.isNotEmpty(sessionIds)) {
            //可选场次必须是启用的场次
            //return sessionMapper.selectBatchIds(sessionIds);

            LambdaQueryWrapper<FlashPromotionSession> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FlashPromotionSession::getStatus, 1);
            queryWrapper.in(FlashPromotionSession::getId, sessionIds);
            return sessionMapper.selectList(queryWrapper);
        }
        return null;
    }

    //修改启用状态
    public void updateFlashSessionStatus(Long id, Integer status) {
        //停用前需要查看是否有使用当前时间段的秒杀活动在上线中
        //状态有两种 0不启用 1启用
        if (status == 1) {
            //状态是1的话 需要查看它所对应的活动 必须要全部下线才行 才可以需改为0
            //获取此段时间所在的商品活动关系的列表
            LambdaQueryWrapper<FlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FlashPromotionProductRelation::getFlashPromotionSessionId, id);
            List<FlashPromotionProductRelation> relations = relationMapper.selectList(wrapper);
            //获取所有的活动id
            if (CollUtil.isNotEmpty(relations)) {
                List<Long> flashPromotionIds = CollUtil.getFieldValues(relations, "flashPromotionId", Long.class);
                //获取所有的秒杀活动
                List<FlashPromotion> flashPromotions = smsFlashMapper.selectBatchIds(flashPromotionIds);
                //将集合中的上下线数据需改为下线
                for (FlashPromotion flashPromotion : flashPromotions) {
                    flashPromotion.setStatus(0);
                    smsFlashMapper.updateById(flashPromotion);
                }
            }
        }
        //修改时间session的状态
        FlashPromotionSession session = new FlashPromotionSession();
        session.setId(id);
        session.setStatus(status);
        sessionMapper.updateById(session);
    }

    //修改场次
    public void updateFlashSession(FlashPromotionSession session) {
        //首先修改状态
        updateFlashSessionStatus(session.getId(), session.getStatus());
        //排除掉状态,修改其他
        session.setStatus(null);
        //修改其他
        sessionMapper.updateById(session);
    }

    //查询场次详情
    public FlashPromotionSession selectFlashSessionInfo(Long id) {
        return sessionMapper.selectById(id);
    }


}
